package com.imay.live.fragment.room;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.adapter.ChooseUserAdapter;
import com.imay.live.proto.user.UserInfoCard;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.live.R;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

/**
 * Created by jeaman on 2017/4/14.
 */

public class ChooseUserFragment extends BaseDialogFragment {
    private RecyclerView mRecyclerView;
    private ChooseUserAdapter mAdapter;
    private List<UserInfoCard> mDataList;

    public static ChooseUserFragment newInstance(List<UserInfoCard> userList) {
        ChooseUserFragment fragment = new ChooseUserFragment();
        fragment.setDataList(userList);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_user;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRecyclerView = $(R.id.choose_user_rv_recycler);

        mAdapter = new ChooseUserAdapter(mDataList);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).colorResId(R.color.divider_f0)
                .sizeResId(R.dimen.divider_size_choose_user)
                .build());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(mAdapter);

        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (getActivity() instanceof OnChooseUserListener) {
                    ((OnChooseUserListener) getActivity()).onChooseUser(mAdapter.getItem(position));
                }
                dismiss();
            }
        });
    }

    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    protected int getHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    };

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    protected boolean isOutsideTouchable() {
        return true;
    }

    public void setDataList(List<UserInfoCard> dataList) {
        mDataList = dataList;
    }

    public interface OnChooseUserListener {
        void onChooseUser(UserInfoCard user);
    }
}
